<template>
  <div class="form-container">
    <div class="form-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>品牌管理</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/brandList' }">品牌列表</el-breadcrumb-item>
        <el-breadcrumb-item>添加品牌</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="form-content">

      <el-form ref="form" :model="form" :rules="rules" label-width="150px">

        <el-form-item label="品牌名称" prop="name">
          <el-col :span="9">
            <el-input v-model="form.name" placeholder="品牌名称"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="品牌类型" prop="type">
          <el-col :span="9">
            <el-radio v-model="form.type" label="1">家居类</el-radio>
            <el-radio v-model="form.type" label="2">泛家居类</el-radio>
          </el-col>
        </el-form-item>
        <el-form-item label="LOGO">
          <el-col :span="9">
            <el-upload
              action="/api/client/base/uploadfile"
              list-type="picture-card"
              :on-preview="handlePictureCardPreview"
              ref="upload"
              :on-remove="handleRemove"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
              :auto-upload="true"
              :limit="50"
              :headers="myHeaders"
              :file-list="fileList"
              accept="image/jpeg, image/jpg, image/gif, image/png"
              :multiple="false">
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
          </el-col>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-col :span="9">
            <el-input v-model="form.sort" placeholder="默认为100,数字越小越靠前"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-row>
            <el-button type="danger" @click="confirmAdd('form')">立即保存</el-button>
            <el-button type="default" @click="cancel()">返回</el-button>
          </el-row>
        </el-form-item>

      </el-form>
    </div>
  </div>

</template>

<script>

  export default {
    data() {
      return {
        validate: false,
        form: {
          type: '1',
          name: '',
          logo: '',
          sort: '100',
        },
        rules: {
          name: [
            {required: true, message: '请输入品牌名称', trigger: 'blur'},
          ],
          sort: [
            {required: true, message: '请输入排序', trigger: 'blur'},
          ],
        },
        fileList: [],
        dialogImageUrl: "",
        dialogVisible: false,
        myHeaders: {
          'access-token': this.$store.state.token, //这里重新定义了请求头
          'Authorization': this.$store.state.token,
          'user-id': this.$store.state.user_id
        },
      }
    },
    mounted() {

    },
    methods: {
      handleRemove(file, fileList) {
        this.fileList = fileList
      },
      handlePictureCardPreview(file) {
        console.log('file', file);
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      handleAvatarSuccess(res, file, fileList) {
        console.log('fileList', fileList);
        this.fileList = fileList
      },
      beforeAvatarUpload(file) {
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
          this.$message.error('上传商品图片大小不能超过 2MB!');
        }
        return isLt2M;
      },

      confirmAdd(form) {
        this.validateFormData(form);
        if (this.validate) {
          const _ths = this;
          if (this.fileList.length > 0) {
            this.fileList.map(v => {
              if (v.response) {
                _ths.form.logo = v.response.data.id;
              } else {
                _ths.form.logo = v.id;
              }
            })
          }
          this.$http.post('/api/root/brand/add', {...this.form})
            .then(rest => {
              if (rest.code == 0) {
                this.$message.success(rest.msg);
                setTimeout(function () {
                  _ths.$router.push({name: 'brandList'})
                }, 800)
              } else {
                this.$message.error(rest.msg);
              }
            })
        }
      },
      cancel() {
        this.$router.push({name: 'brandList'})
      },

      /**
       * 验证表单数据
       * @param form
       */
      validateFormData(form) {
        this.$refs[form].validate((valid) => {
          if (!valid) {
            this.$message({
              message: '表单数据未填写完整,操作失败！',
              type: 'warning'
            });
            return false;
          }
          this.validate = true;
          return true;
        })
      },
    }
  }
</script>
<style lang="less" scoped>
  @import "index.less";
</style>